package com.sky.controller.admin;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.admin.DishService;
import com.sky.utils.RedisCleanCacheUtil;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.type.Alias;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("admin/dish")
@Slf4j
@Api(tags = "菜品模块相关接口")
@Alias("adminDishController")
public class DishController {

    @Autowired
    private DishService dishService;


    @GetMapping("page")
    @ApiOperation("菜品模块分页查询的业务接口")
    public Result<PageResult> getDishByPage(DishPageQueryDTO dto) {
        log.info("前端菜品Dto类：{}", dto);
        Result<PageResult> result = dishService.getDishByPage(dto);

        return result;
    }



    @PostMapping
    @ApiOperation("增加菜品信息")
    @CacheEvict(cacheNames = "userDishCache" ,key = "#dto.categoryId") // 请求分类下菜品缓存
    public Result<Object> addDish(@RequestBody DishDTO dto) {
        log.info("菜品增加接口传入数据：{}",dto);
        Result<Object> result = dishService.addDish(dto);

        return result;
    }

    @GetMapping("list")
    @ApiOperation("根据分类id查询菜品")
    public Result<List<Dish>> getDishListByTypeId(@RequestParam("categoryId") Long categoryId){
        log.info("根据分类id查询菜品接口参数：{}",categoryId);

        Result<List<Dish>> result = dishService.getDishListByCategoryId(categoryId);

        return result;
    }

    @PostMapping("status/{status}")
    @ApiOperation("菜品起售、停售的业务接口")
    @CacheEvict(cacheNames = "userDishCache" ,allEntries = true)
    public Result<Object> setDishStatus(@PathVariable Long status,@RequestParam("id") Long id){
        log.info("设置菜品状态：{},{}",status,id);

        Result<Object> result = dishService.setDishStatusById(status,id);

        return result;
    }

    @GetMapping("{id}")
    @ApiOperation("根据id查询菜品的业务接口")
    public Result<Object> getDishInfoById(@PathVariable Long id){
          log.info("查询菜品id为：{}",id);

          return dishService.getDishInfoById(id);

    }

    @PutMapping
    @ApiOperation("修改菜品的业务接口")
    @CacheEvict(cacheNames = "userDishCache" ,allEntries = true)
    public Result<Object> updateDishInfo(@RequestBody DishVO vo){
        Result<Object> result = dishService.updateDishInfo(vo);


        return result;
    }


    @DeleteMapping
    @ApiOperation("批量删除菜品的业务接口")
    @CacheEvict(cacheNames = "userDishCache" ,allEntries = true)
    public Result<Object> deleteDishByIds(@RequestParam("ids") String ids){
        Result<Object> result = dishService.deleteDishByIds(ids);
        return result;
    }

}
